package com.goodwill.hdr.config;


import com.goodwill.hdr.Strategy.HbaseFilterStrategy;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class BeanConfig {
    private Logger logger = LoggerFactory.getLogger(BeanConfig.class);
    private final HbaseFilterStrategy hbaseEq;
    private final HbaseFilterStrategy hbaseNotEq;
    private final HbaseFilterStrategy hbaseIn;
    private final HbaseFilterStrategy hbaseNotIn;

    private final HbaseFilterStrategy hbaseGe;
    private final HbaseFilterStrategy hbaseGt;
    private final HbaseFilterStrategy hbaseLe;
    private final HbaseFilterStrategy hbaseLt;
    @Value("${zkHost}")
    private String zkHost;
    @Value("${solrCollection}")
    private String solrCollection;


    public BeanConfig(HbaseFilterStrategy hbaseEq, HbaseFilterStrategy hbaseNotEq, HbaseFilterStrategy hbaseIn, HbaseFilterStrategy hbaseNotIn, HbaseFilterStrategy hbaseGe, HbaseFilterStrategy hbaseGt, HbaseFilterStrategy hbaseLe, HbaseFilterStrategy hbaseLt) {
        this.hbaseEq = hbaseEq;
        this.hbaseNotEq = hbaseNotEq;
        this.hbaseIn = hbaseIn;
        this.hbaseNotIn = hbaseNotIn;
        this.hbaseGe = hbaseGe;
        this.hbaseGt = hbaseGt;
        this.hbaseLe = hbaseLe;
        this.hbaseLt = hbaseLt;
    }


    @Bean(destroyMethod = "close")
    public Connection getConnection() {
        try {
            return ConnectionFactory.createConnection();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Bean(name = "hbaseFilterStrategyMap")
    public Map<String, HbaseFilterStrategy> hbaseFilterStrategyMap() {
        Map<String, HbaseFilterStrategy> map = new HashMap<>();
        map.put("=", hbaseEq);
        map.put("<>", hbaseNotEq);
        map.put("in", hbaseIn);
        map.put("nin", hbaseNotIn);
        map.put(">", hbaseGt);
        map.put(">=", hbaseGe);
        map.put("<", hbaseLt);
        map.put("<=", hbaseLe);
        return map;
    }

    @Bean(name = "cloudSolr")
    public CloudSolrServer getCloudSolr() {

        logger.info("zkhost" + zkHost);
        logger.info("solrCollection" + solrCollection);

        CloudSolrServer solr = new CloudSolrServer(zkHost);
        solr.setDefaultCollection(solrCollection);
        solr.setZkClientTimeout(20000);
        solr.setZkConnectTimeout(20000);
        solr.connect();
        return solr;
    }
}
